package com.it.viewdraghelper;

import android.content.Context;
import android.support.v4.widget.ViewDragHelper;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.widget.LinearLayout;

public class VDHLayout extends LinearLayout {

     public VDHLayout(Context context) {
        this(context, null);
         initViewDragHelper();
    }

    public VDHLayout(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
        initViewDragHelper();

    }

    public VDHLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initViewDragHelper();

    }


    private ViewDragHelper viewDragHelper;

    private void initViewDragHelper(){
        viewDragHelper = ViewDragHelper.create(this,myCallBack);
        viewDragHelper.setEdgeTrackingEnabled(ViewDragHelper.EDGE_ALL);
    }

    ViewDragHelper.Callback myCallBack = new ViewDragHelper.Callback() {

        @Override
        public void onViewCaptured(View capturedChild, int activePointerId) {
            super.onViewCaptured(capturedChild, activePointerId);
        }

        @Override
        public boolean tryCaptureView(View child, int pointerId)
        {
            return true;
        }
        @Override
        public int clampViewPositionHorizontal(View child, int left, int dx)
        {
            return left;
        }
        @Override
        public int clampViewPositionVertical(View child, int top, int dy)
        {
            return top;
        }
    };



    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        return viewDragHelper.shouldInterceptTouchEvent(ev);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        viewDragHelper.processTouchEvent(event);
        return true;
    }






}
